<?php

class InstantMessage 
{
    protected $ws = null;
    protected $wsHost = '0.0.0.0';
    protected $wsPort = '56732';

    /**
     * @title 下个版本 将加入redis
     * 存储用户信息：uid、vartar、nickname、ip、city等
     * 后台数据统计：进入聊天室次数、在聊天室时长、发言次数
     * 记录聊天内容，用于客户需求分析
     */
    // protected $redis = null;
    // protected $redisHost = "127.0.0.1";
    // protected $redisAuth = "footmark";

    public function __construct($config = [])
    {
        $this->wsHost = $config['ws_host'] ?? $this->wsHost;
        $this->wsPort = $config['ws_port'] ?? $this->wsPort;
        // $this->redisHost = $config['redis_host'] ?? $this->redisHost;
        // $this->redisAuth = $config['redis_auth'] ?? $this->redisAuth;

        $this->ws = new Swoole\WebSocket\Server($this->wsHost, $this->wsPort);
        // $this->redis = new Redis();
        // $this->redis->connect($this->redisHost);
        // $this->redis->auth();
    }

    public function start()
    {
        $this->ws->on('open', function($ws, $request){
            // var_dump($request->fd, $request->get, $request->server);
            echo "有新伙伴连接进来了";
            // 更新聊天室人数
            foreach($ws->connections as $fd){             
                $ws->push($fd, json_encode(array('type'=>'numbers','content'=>count($ws->connections))));
            }
            
        });

        // 监听WebSocket消息事件
        $this->ws->on('message', function ($ws, $frame) {

            echo "Message: {$frame->data}\n";

            $msg = json_decode($frame->data, true);
            var_dump($msg);

            switch($msg['type']){
                case 'start':
                    // 有新伙伴进入聊天室
                    // 在群里提示有新伙伴加入
                    foreach($ws->connections as $fd){
                        if($frame->fd != $fd){ // 不用给新加入者推送他自己的入群提醒
                            $ws->push($fd, json_encode(array('type'=>'welcome','content'=>$msg['nickname'])));
                            $ws->push($fd, json_encode(array('type'=>'message','content'=>$msg['content'],'vartar'=>$msg['vartar'])));
                        }
                    }
                break;

                case 'message':
                    // 收到普通消息
                    // 把收到的消息广播出去
                    foreach($ws->connections as $fd){
                        if($frame->fd != $fd){  // 不需要再把消息推送给消息的发出者
                            $ws->push($fd, json_encode(array('type'=>'message','content'=>$msg['content'],'vartar'=>$msg['vartar'])));
                        }
                    }
                break;

                default:
                    echo "收到不明消息...";
                    
            }
        });

        //监听WebSocket连接关闭事件
        $this->ws->on('close', function ($ws, $fd) {
            echo "client-{$fd} is closed\n";
            // 更新聊天室人数, 回调函数执行完毕才会执行close
            // 所以不能在回调函数中更新聊天室的人数

        });

        $this->ws->start();
    }
}
?>
